<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

require_once('../../_inc/funct.getAbilitaz.php');
$aAbilitaz = getAbilitaz();

$aCamForm = array();
require("_inc/const.campiRuolo.php");

if (reqVar('azione') == 'salva') {
	$msgPag = confIns($aCamForm);
} else {
	$nn=0; $aIndA=array();
	foreach($aAbilitaz as $abX => $aAbX) {
		$nn++;
		$aIndA[$abX] = $nn;
		$_REQUEST["abilitaz_val_".$nn] = $aAbX['def'];
		$_REQUEST["abilitaz_cod_".$nn] = $abX;
	}
}
testaHtml('Gestione applicazione',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Utente</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="tit1" colspan='5' align='center'>
			Abilitazioni
		</td>
   </tr>
	<tr>
		<td class="lab1" width='40%' align='center'>Descrizione</td>
		<td class="lab1" width='50%' align='center'>Flag</td>
		<td class="lab1" width='10%' align='center'>Codice</td>
   </tr>
<?
$clRF='txt2';
$p1 = $p2 = 0;
foreach($_REQUEST as $kX => $abX) {
	if (substr($kX,0,13)!='abilitaz_cod_') continue;
	$nn = substr($kX,13);
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$aValAbX = array();
	$aAbX = $aAbilitaz[$abX];
	foreach($aAbX['val'] as $vX => $aX) {
		$aValAbX[$vX] = $vX." - ".$aX['des'];
	}
	if (strlen($abX)>4) {
		$p2++;
		$indent="<td class='$clRF' width='30'>&nbsp;</td>";
		$numeraz = $p1.".".$p2." -&nbsp;";
	} else {
		$p1++;
		$p2=0;
		$indent = "";
		$numeraz = $p1." -&nbsp;";
	}
	$bold="font-weight:bold;";
?>
	<tr>
		<td class="<?=$clRF?>" align='left' valign='top'>
			<table cellspacing='0' cellpadding='0'>
				<tr><?=$indent?>
					<td align='right' class='<?=$clRF?>' width='25'><?=$numeraz?></td>
					<td style='<?=$bold?>' class="<?=$clRF?>"><?=$aAbX['des']?></td>
				</tr>
				<tr><?=$indent?>
					<td align='right' class='<?=$clRF?>'>&nbsp;</td>
					<td class="<?=$clRF?>"><?=$aAbX['not']?></td>
				</tr>
			</table>
		</td>
		<td class="<?=$clRF?>" align='left' valign='top'>
			<?
			echo getHtmlHidden('abilitaz_cod_'.$nn);
			echo getHtmlRadio('abilitaz_val_'.$nn,$aValAbX,'@REQ',array('aCapo'=>true))?>
		</td>
		<td class="<?=$clRF?>" style='font-family:lucida console,courier;' valign='top'>
			<?=$abX?>
		</td>
	</tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function confIns($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_ruolo'] = strtoupper(trim(reqVar('cod_ruolo')));
	if (reqVar('cod_ruolo')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (preg_match("/[^-_A-Z0-9]/",reqVar('cod_ruolo'))) {
		$msg = "Codice '".reqVar('cod_ruolo')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_ruolo FROM rgl_ruoli " .
			" WHERE cod_ruolo = '".reqVarAS('cod_ruolo')."'";
	$objDB->exec($sqlSel,'insIst');
	$aRow = $objDB->fetch('insIst');
	if ($aRow) {
		$msg .= "Codice ruolo gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlIns = "INSERT INTO rgl_ruoli SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			$sSqlIns .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
		}
	}
	$sSqlIns = substr($sSqlIns,0,-2);
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');

	foreach($_REQUEST as $kX => $abX) {
		if (substr($kX,0,13)!='abilitaz_cod_') continue;
		$nn = substr($kX,13);
		$vX = $_REQUEST['abilitaz_val_'.$nn];
		$sSql = "INSERT INTO rgl_ruoli_abilitaz SET id_ruolo = " . $newId.
				", cod_abilitaz = '".addslashes($abX)."'".
				", valore = '".addslashes($vX)."'";
		$objDB->exec($sSql);
	}
		
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: ".urlTorna("ruolo '$cod_ruolo' inserito"));
	exit();
}
?>